/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using LaunchySharp;

namespace LaunchyZilla
{
    public class LaunchyZilla : IPlugin
    {
		private IPluginHost m_pluginHost;
		private ICatItemFactory m_catItemFactory;
        private ILaunchyPaths m_launchyPaths;
        private string m_iconPath;
		private uint m_id;
        private uint m_labelHash;
        private string m_name;

		public void init(IPluginHost pluginHost)
		{
			m_pluginHost = pluginHost;
			if (m_pluginHost != null) {
				m_catItemFactory = m_pluginHost.catItemFactory();
			    m_launchyPaths = m_pluginHost.launchyPaths();
			}
		    m_name = "LaunchyZilla#";
			m_id = m_pluginHost.hash(m_name);
		    m_labelHash = m_id;
            m_iconPath = m_launchyPaths.getIconsPath() + "\\filezilla.ico";
		}
		
		public uint getID()
		{
			return m_id;
		}
		
		public string getName()
		{
			return m_name;
		}

        public string getIcon()
        {
            return m_iconPath;
        }
		
		public void getLabels(List<IInputData> inputDataList)
		{
            if(inputDataList.Count != 2)
            {
                return;
            }

		    string lowerText = inputDataList[0].getText().ToLower();
            if(lowerText == "ftp")
            {
                inputDataList[0].setLabel(m_labelHash);
            }
		}
		
		public void getResults(List<IInputData> inputDataList, List<ICatItem> resultsList)
		{
		    if (!inputDataList[0].hasLabel(m_labelHash))
            {
                return;
            }

		    string ftpSessionToMatch = inputDataList[1].getText();
		    int matchCount = addSessionsToList(resultsList, ftpSessionToMatch);

            if(matchCount == 0)
                resultsList.Insert(0,
                                   m_catItemFactory.createCatItem(ftpSessionToMatch + ".cmdline", ftpSessionToMatch,
                                                                  getID(), getIcon()));
		}
		
		public void getCatalog(List<ICatItem> catalogItems)
		{
            catalogItems.Add(m_catItemFactory.createCatItem("ftp.LaunchyZilla#", "ftp", getID(), getIcon()));
		    addSessionsToList(catalogItems, "");
		}
		
		public void launchItem(List<IInputData> inputDataList, ICatItem item)
		{
			ICatItem catItem = 
				inputDataList[inputDataList.Count-1].getTopResult();
		    string fullPath = catItem.getFullPath();
		    string type = fullPath.Substring(fullPath.LastIndexOf('.') + 1);

		    string args = (type == "sitemanager" ? string.Format("-c \"{0}\"", fullPath.Replace(".sitemanager", "")) : catItem.getShortName());
            try
            {
                //MessageBox.Show("I would launch: " + Environment.ExpandEnvironmentVariables(Properties.Settings.Default.FileZilla_Path) + " " + args);
                Process.Start(Environment.ExpandEnvironmentVariables(Properties.Settings.Default.FileZilla_Path), args);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error launching: " + ex.Message);
            }
		}

        private int addSessionsToList(IList<ICatItem> catalogItems, string matchText)
        {
            List<FileZillaSite> sites = FileZillaSite.GetSites();

            List<FileZillaSite> matchedSites = !string.IsNullOrEmpty(matchText) ? FileZillaSite.FilterSites(sites, matchText) : sites;

            foreach (FileZillaSite site in matchedSites)
            {
                catalogItems.Insert(0, m_catItemFactory.createCatItem(site.Path + ".sitemanager", site.Name, getID(), getIcon()));
            }

            return matchedSites.Count;
        }
		
		public bool hasDialog()
		{
			return false;
		}

		public IntPtr doDialog()
		{
			return IntPtr.Zero;
		}

		public void endDialog(bool acceptedByUser)
		{
		}

		public void launchyShow()
		{
		}

		public void launchyHide()
		{
		}
	}
}
